<?php

/**
 * 脏字过滤
 * Date: 2018/12/20
 */

namespace Lib\Paas;

class DirtyFilter extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 检查脏字
     * @param string $words【必填】 待检查的内容 必须是UTF-8编码
     * @param string $clearWords【必填】 引用传递，返回过滤脏字之后的内容
     * @return bool true - 不包含脏字  false-包含脏字
     */
    public function checkDirtyWord($words, &$clearWords)
    {
        //判断是否为空
        if (!$words) {
            $clearWords = '';
            return true;
        }

        //组合URL
        $url = $this->getPaasRequestUrl('tool.checkdirty', [], false);

        $res = $this->http->post($url, $words);
        recordLog("dirty words check return data =" . var_export($res, true), __FILE__, __LINE__);
        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'tool.checkdirty');
            return false;
        }
        $res = $res['data'];

        //接口出现异常，为了确保安全，返回校验不通过
        if ($res['ret'] != 0 || $res['data']['ret'] != 0) {
            $clearWords = '';
            return false;
        }
        //包含脏字
        if ($res['data']['isDirty'] == '1') {
            $clearWords = $res['data']['data']['msg_'];
            return false;
        } else {
            $clearWords = $words;
        }

        return true;
    }
}
